﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Models;

namespace Data
{
    public class UsersData : DataBase
    {

        public void UpdateUser(Users user)
        {
            var parametrs = new List<SqlParameter>
                                {
                                    new SqlParameter("@Id", user.Id),
                                    new SqlParameter("@Login", user.Login),
                                    new SqlParameter("@Pwd", user.Pwd),
                                    new SqlParameter("@Email", user.Email),
                                    new SqlParameter("@Role", user.Role),
                                    new SqlParameter("@Name", user.Name),
                                    new SqlParameter("@FName", user.FName),
                                    new SqlParameter("@MName", user.MName),
                                };
            ExecuteNonQuery("UpdateUser", parametrs);
        }

        public void DeleteUser(int id)
        {
            ExecuteNonQuery("DeleteUser", new List<SqlParameter> {new SqlParameter("@Id", id)});
        }

        public List<Users> GetUsers(int id = 0, int role = 0, string login = null, string orderBy = null)
        {
            var parametrs = new List<SqlParameter>
                                {
                                    new SqlParameter("@id", id),
                                    new SqlParameter("@login", login),
                                    new SqlParameter("@Role", role),
                                    new SqlParameter("@orderBy", orderBy)
                                };
            var dataTable = ExecuteDataTable("GetUsers", parametrs);

            if (dataTable.Rows.Count == 0) return null;
            var dbImages = new ImagesData();
            var usersList = new List<Users>();
            foreach (DataRow row in dataTable.Rows)
            {
                const string offline = "/Content/images/user_offline.png";
                const string online = "/Content/images/user_online.png";
                usersList.Add(new Users
                                  {
                                      Id = Convert.ToInt32(row["ID"]),
                                      Login = Convert.ToString(row["Login"]),
                                      Role = Convert.ToInt32(row["Role"]),
                                      Email = Convert.ToString(row["Email"]),
                                      Image = (dbImages.GetUserImagesUrl(Convert.ToInt32(row["ID"])) == null) ? null : dbImages.GetUserImagesUrl(Convert.ToInt32(row["ID"]))[0],
                                      OnlineOfflineImg = (DateTime.Now.AddMinutes(-10) > (row["LastVisit"] as DateTime?) ) ? offline :online,
                                      Pwd = Convert.ToString(row["Pwd"]),
                                      Name = Convert.ToString(row["Name"]),
                                      FName = Convert.ToString(CheckDbNull(row["FName"])),
                                      MName = Convert.ToString(CheckDbNull(row["MName"]))
                                  });
            }

            return usersList;
        }

        public int AddUser(Users user)
        {
            var parametrs = new List<SqlParameter>
                                {
                                    new SqlParameter("@Login", user.Login),
                                    new SqlParameter("@Name", user.Name),
                                    new SqlParameter("@FName", user.FName),
                                    new SqlParameter("@MName", user.MName),
                                    new SqlParameter("@Email", user.Email),
                                    new SqlParameter("@Pwd", user.Pwd),
                                    new SqlParameter("@Role", user.Role),
                                };

            return Convert.ToInt32(ExecuteScalar("AddUser", parametrs));
        }

        public void UpdateLastVisit(int? id = null, string login = null)
        {
            var parameters = new List<SqlParameter>
                                 {
                                     new SqlParameter("@ID", id),
                                     new SqlParameter("@Login", login)
                                 };
            ExecuteNonQuery("UpdateLastVisit",parameters);
        }
    }
}